﻿using System.Security.Cryptography;
using System.Text;

namespace HelperCommon.Helper
{
    public class MD5Helper
    {
        #region MD5加密列

        /// <summary>
        /// 16位加密
        /// </summary>
        /// <param name="input">加密内容</param>
        /// <returns></returns>
        public static string MD5Encrypt16(string input)
        {
            MD5 md5 = MD5.Create();
            var m1 = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input)), 4, 8);
            m1 = m1.Replace("-", string.Empty);
            return m1;
        }

        /// <summary>
        /// 32位加密
        /// </summary>
        /// <param name="input">加密内容</param>
        /// <returns></returns>
        public static string MD5Encrypt32(string input)
        {
            MD5 md5 = MD5.Create();
            var m1 = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input)));
            m1 = m1.Replace("-", string.Empty);
            return m1;
        }

        /// <summary>
        /// 64位加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string MD5Encrypt64(string input)
        {
            MD5 md5 = MD5.Create();
            var m1 = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(input)));
            return m1;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">加密内容</param>
        /// <param name="type">加密类型:16.32,base64,默认32</param>
        /// <returns></returns>
        public static string MD5Encrypt(string input, int type = 32)
        {
            MD5 md5 = MD5.Create();
            string m1 = string.Empty;
            switch (type)
            {
                case 16:
                    return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input)), 4, 8).Replace("-", string.Empty);

                case 32:
                    return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input))).Replace("-", string.Empty);

                case 64:
                    return Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(input))).ToUpper();

                default:
                    // 默认转换为32位的十六进制字符串
                    return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input))).Replace("-", string.Empty);
            }
        }

        #endregion MD5加密列

        #region SHA256加密列

        /// <summary>
        /// SHA256加密
        /// </summary>
        /// <param name="input">加密内容</param>
        /// <returns></returns>
        public static string SHA256Encrypt(string input)
        {
            SHA256 sHA256 = SHA256.Create();
            var s1 = BitConverter.ToString(sHA256.ComputeHash(Encoding.UTF8.GetBytes(input))).Replace("-", string.Empty);
            return s1;
        }

        #endregion SHA256加密列
    }
}